package com.chasmcity.munrobagging;

import static java.util.Collections.*;
import android.app.ListActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Filterable;
import android.widget.AdapterView.OnItemClickListener;

import com.chasmcity.munrobagging.model.Munro;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * TODO: Missing class description for MunroListActivity!
 * <p>
 * &copy; Copyright 2011 Kizoom Ltd.
 * <p>
 * $Id: wildebeest_standard.epf,v 1.9 2007/01/17 14:59:59 danf Exp $
 */
public class MunroListActivity extends ListActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.munro_list);
		
		populateList(NAME_ASC);

		EditText filterText = (EditText) findViewById(R.id.munroListFilter);
	    filterText.addTextChangedListener(new FilterTextWatcher());
		getListView().setTextFilterEnabled(true);
		
		getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Munro munro = (Munro) getListAdapter().getItem(position);
				MunroDetailsActivity.launch(MunroListActivity.this, munro);
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.munro_list_menu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.sortByName:
			populateList(NAME_ASC);
			return true;
		case R.id.sortByHeight:
			populateList(HEIGHT_DESC);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		} 
	}

	private void populateList(Comparator<Munro> sort) {
		List<Munro> munros = new ArrayList<Munro>(MunroBaggingApp.getMunros(this));
		sort(munros, sort);
		setListAdapter(new ArrayAdapter<Munro>(this, R.layout.munro_list_item, munros));
	}
	
	private static final Comparator<Munro> NAME_ASC = new Comparator<Munro>() {
		@Override
		public int compare(Munro m1, Munro m2) {
			return m1.getName().compareToIgnoreCase(m2.getName());
		}
	};
	
	private static final Comparator<Munro> HEIGHT_DESC = new Comparator<Munro>() {
		@Override
		public int compare(Munro m1, Munro m2) {
			return m2.getHeightInMetres() - m1.getHeightInMetres();
		}
	};

	private class FilterTextWatcher implements TextWatcher {

		@Override
		public void afterTextChanged(Editable s) {}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			((Filterable)getListAdapter()).getFilter().filter(s);
		}
		
	}
}
